$(document).ready(function() {

    //second level
    function goDown () {
        $('ul.navtwo', this).css('display', 'block');
        $(this).children('.rubrika').not('.search a').addClass('hover');
        newsTickerObj.nTicker('setSpeed', 0);
    }
    function goUp () {
        $('ul.navtwo', this).css('display', 'none');
        $(this).children('.rubrika').removeClass('hover');
        newsTickerObj.nTicker('setSpeed', 1);
    }
    $("#nav li").hoverIntent
    ({
        sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)
        interval: 200, // number = milliseconds for onMouseOver polling interval
        over: goDown, // function = onMouseOver callback (REQUIRED)
        timeout: 300, // number = milliseconds delay before onMouseOut
        out: goUp // function = onMouseOut callback (REQUIRED)
    })

    $("#ul.nav_toarticle").hoverIntent
    ({
        sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)
        interval: 200, // number = milliseconds for onMouseOver polling interval
        over: arrIn, // function = onMouseOver callback (REQUIRED)
        timeout: 300, // number = milliseconds delay before onMouseOut
        out: arrOut // function = onMouseOut callback (REQUIRED)
    })

    // to article submeni
    function arrIn()
    {
        $(this).parent().next('ul.nav_toarticle').css('display', 'block');
        $(this).parent().addClass('ar_hover');
    }
    function arrOut()
    {
        console.log($(this).parent().next('ul.nav_toarticle').hover())
        if ( $(this).parent().next('ul.nav_toarticle').hover() )
            {
            return false;
        }
        else
            {
            $(this).parent().next('ul.nav_toarticle').css('display', 'none');
            $(this).parent().removeClass('ar_hover');
        }
    }


    $('ul.nav_toarticle').hover(
    function() {
        $(this).css('display', 'block');
        $(this).prev('a').addClass('nav_hover');
    },
    function() {
        $(this).css('display', 'none');
        $(this).prev('a').removeClass('nav_hover');
    }); 


    // to article submeni
    var empty_subsections = {
        'djevojka-dana' : true,
        'sexy-fotke' : true,
        'sexy-video' : true
    };

    function goDownNsa() {
        var current_li = $(this); //.parent('a').parent('li');
        var this_subsection = $(current_li);
        var subsection_a = this_subsection.children('a:first');

        if (subsection_a.hasClass('podrubrike')) {
            var subsection_slug = subsection_a.attr('rel');
            var nav_to_article = $('.nav_toarticle', this_subsection);
            var already_loaded = nav_to_article.length > 0;

            subsection_a.addClass('nav_hover');

            if (already_loaded) {
                nav_to_article.css('display', 'block');
            } else {
                if (!empty_subsections[subsection_slug]) {
                    subsection_a.append($("<img width='15' height='15' style='float:right;margin-top:11px;margin-right:33px;' src='"+base_url+"assets/images/ajax-loader.gif'/>"));

                    $.ajax({
                        url : base_url+'articles/newsFeed/' + subsection_slug,
                        success : function (data) {
                            var html_data = $(data);

                            if (subsection_a.hasClass('nav_hover')) {
                                html_data.css('display', 'block');
                            }

                            this_subsection.append(html_data);
                            subsection_a.children('img').remove();
                        },
                        error : function (jqXHR, textStatus, errorThrown) {
                            empty_subsections[subsection_slug] = true;
                            subsection_a.children('img').remove();
                        }
                    });
                }
            }

            $('.nav_to_article', this_subsection)

        }
    }
    function goUpNsa() {
        current_li = $(this); //.parent('a').parent('li');
        var this_subsection = $(current_li);
        var subsection_a = this_subsection.children('a:first');
        var nav_to_article = $('.nav_toarticle', this_subsection);
        nav_to_article.css('display', 'none');
        subsection_a.removeClass('nav_hover');

    }

    $('ul.navtwo li').hoverIntent({
        sensitivity : 1, // number = sensitivity threshold (must be 1 or higher)
        interval : 200, // number = milliseconds for onMouseOver polling interval
        over : goDownNsa, // function = onMouseOver callback (REQUIRED)
        timeout : 300, // number = milliseconds delay before onMouseOut
        out : goUpNsa// function = onMouseOut callback (REQUIRED)
    });


    function ticker2Dim () {
        var boxWidth = $('#ticker_v3 span.title').width();
        $('#ticker_v3 #viewport').css('width', 935-boxWidth+'px');
    }
    ticker2Dim ();
    // ticker settings
    var newsTickerObj=$('.newsticker').nTicker();
    var newsTickerObjVer=$('.liveticker').nTicker({vertical: true});
    $(".nticker").newsTicker();
    $('#decrease_button')
    .mousedown( function() { newsTickerObj.nTicker('setSpeed', -3); } )
    .mouseup( function() { newsTickerObj.nTicker('setSpeed', 1); } )
    .click( function() { return false; } ) ;
    $('#increase_button')
    .mousedown( function() { newsTickerObj.nTicker('setSpeed', 3); } )
    .mouseup( function() { newsTickerObj.nTicker('setSpeed', 1); } )
    .click( function() { return false; } ) ; 


    // initialize scrollable for home page
    /*
    $(".scroll").scrollable(
    {
    keyboard: false,
    circular: true,
    item: '.kocka2',
    autoplay: true
    //interval: 2000    	
    }
    );
    */
    var root = $("#novevijesti")
    .scrollable(
    {circular: true, item: '.kocka2' })
    .autoscroll(
    { autoplay: true, interval: 5000	 }
    );

    // provide scrollable API for the action buttons
    window.api = root.data("scroll");

    root.each(function() {
        var $itemsToClone = $(this).scrollable().getItems().slice(1);
        var $wrap = $(this).scrollable().getItemWrap();
        var clonedClass = $(this).scrollable().getConf().clonedClass;
       
	    $itemsToClone.each(function() {
	        $(this).clone(true).appendTo($wrap)
	        .addClass(clonedClass + ' hacked-' + clonedClass);
	    });
           
        
    });
    
    
    

    var bColor;

    $('.mPhoto').hover(
    function() {

        bColor = $(this).parent().css('border-bottom'); 
        $(this).parent().css('border-bottom', '2px solid #dedede'); 
        $(this).parent().next().next().find('a.mLink').css('color', '#FD1900');
    },
    function() {
        $(this).parent().css('border-bottom', bColor);
        $(this).parent().next().next().find('a.mLink').css('color', '#222222'); 
    });

    $('.mLink').hover(
    function() {
        bColor = $(this).parent().parent().children('div:first').css('border-bottom');
        $(this).parent().parent().children('div:first').css('border-bottom', '2px solid #dedede');
        $(this).css('color', '#FD1900');
    },
    function() {
        $(this).parent().parent().children('div:first').css('border-bottom', bColor);
        $(this).css('color', '#222222'); 
    });

    $('.kocka2').hover(
    function() {
        bColor = $(this).find('.kocka2img').css('border-bottom');
        $(this).find('.kocka2img').css('border-bottom', '2px solid #dedede');
        $(this).find('h4 a.mLink').css('color', '#FD1900');
        $(this).animate({top: '-4'}, 100, 'linear'); 
    },
    function() {
        $(this).animate({top: '0'}, 100, 'linear');
        $(this).find('h4 a.mLink').css('color', '#222222');
        $(this).find('.kocka2img').css('border-bottom', bColor);
    }); 


    /*if(sub=='home'){
    // CALL SHOW PROMO
    window.setTimeout(function() {  slide_news(); }, 2001);  
    }

    function slide_news(){
    //console.log('click')
    $(".scroll_button").trigger('click');
    window.setTimeout(function() {  slide_news(); }, 2001);
    } */


    /*  HOROSKOPSKI TABOVI  */
    $("ul.tabs").tabs("div.panes > div");


    /* DODAVANJE KOMENTARA */
    $("#insert_comment").click(function(){

        var ajaxURL = base_url + "comments/insertComment";

        $.ajax({
            type: "POST",
            url: ajaxURL,
            data: $('#form_add_comment').serialize(),
            async: true,
            success: function(response){

                var result = response.split('|');
                if(result[0])
                    {
                    $("#new_user_comment").html(result[2]);
                }
                //alert(result[0]);
                alert(result[1]);
            }
        }); //end ajax
    });



    /* Scrolanje social button-a */
    $('#social').scrollToFixed();

    var _popup_ajax_loader = '<img src="'+base_url+'assets/images/ajax-loader.gif" />';

    popupclose = function () {
        $('#popup').fadeOut(500, function(){
            $('#popup #popupbox .popuptable #popupboxitself #content').html(_popup_ajax_loader);
        });
    }
    do_popup = function (data) {
        $('#popup').fadeIn(500, function () {
            $('#popup #popupbox .popuptable #popupboxitself #content').load(data.url);
        });
    }
    
    
    $(".tabs_item").click(function(){
    	$(".tabs_item").removeClass('current');
    	$(this).addClass('current');
    });
    

});

if(sub=='home'){
    $(window).load(function() {
        for(i=1;i<=1;i++){ 
            $('#slider-'+i).nivoSlider({
                effect:'fade', //Specify sets like: 'random,fold,fade,sliceDown'
                slices:1,
                animSpeed:500, //Slide transition speed
                pauseTime:7000,
                captionOpacity:0, //Universal caption opacity 
                pauseOnHover:true,
                directionNav:false, //Next & Prev
                controlNav:false //1,2,3...
            });
        }          
    });
    
    
    
}


function article_subsection(sec,sub)
{
	
	$("#scroller_items").html("");
	$("#loading").css("display","block");
	 $("#empty").css("display","none");
	 var ajaxURL = base_url + "articles/getArticlesForScrollerOnArticlePage/"+sec+"/"+sub;
	 $("#scroller_items").html("<div class='loading'>Ucitava se...</div>");
     $.ajax({
         type: "POST",
         url: ajaxURL,
         //data: "column="+sec+"&subcolumn="+sub,
         async: true,
         success: function(response){
        	 
        	 //document.write(response);
        	 //return;
        	 $("#loading").css("display","none");
        	
        	 if(response!="")
        	 {
        		 $("#scroller_items").html(response)
        		 
        		 
        		 window.api = root.data("scroll");

    		    root.each(function() {
    		        var $itemsToClone = $(this).scrollable().getItems().slice(1);
    		        var $wrap = $(this).scrollable().getItemWrap();
    		        var clonedClass = $(this).scrollable().getConf().clonedClass;
    		       
    			    $itemsToClone.each(function() {
    			        $(this).clone(true).appendTo($wrap)
    			        .addClass(clonedClass + ' hacked-' + clonedClass);
    			    });
    		           
    		        
    		    });
        		 
        	 }
        	 else
        		 $("#empty").css("display","block");
        	 
         } //end success
     }); //end ajax
     	
}